﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.ComponentModel;

namespace NutriLeaf.Phone.Pages.Tips
{
    public partial class Tips : PhoneApplicationPage,INotifyPropertyChanged
    {

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } }
        #endregion

        TipSync.TipSyncOfflineContext tipContext = new TipSync.TipSyncOfflineContext(Constants.SyncCache.Tips,new Uri(Constants.SyncServices.Tips));

        public List<TipSync.TipCategories> tipCategories;

        public List<TipSync.TipCategories> TipCategories
        {
            get
            {
                return tipCategories;
            }
            set
            {
                tipCategories = value;
                NotifyPropertyChanged("TipCategories");
            }
        }

        public List<TipSync.Tips> tips;

        public List<TipSync.Tips> Tip
        {
            get
            {
                return tips;
            }
            set
            {
                tips = value;
                NotifyPropertyChanged("Tips");
            }
        }

        public Tips()
        {
            InitializeComponent();
        }

        


        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            LoadSyncContext();
            
            
        }

        private void LoadSyncContext()
        {
            tipContext.LoadCompleted += new EventHandler<Microsoft.Synchronization.ClientServices.IsolatedStorage.LoadCompletedEventArgs>(tipContext_LoadCompleted);
            tipContext.LoadAsync();
        }

        void tipContext_LoadCompleted(object sender, Microsoft.Synchronization.ClientServices.IsolatedStorage.LoadCompletedEventArgs e)
        {
            GetTipCategories();
            GetTips();
        }

        private void GetTips()
        {
            Tip = tipContext.TipsCollection.ToList();
        }

        

        

        private void SetDataContext()
        {
            
        }

        private void GetTipCategories()
        {
            TipCategories = tipContext.TipCategoriesCollection.ToList();
            SetTipCategoriesItemSource(0);
        }

        


        public void SetTipCategoriesItemSource(int parentId)
        {

            Dispatcher.BeginInvoke(() =>
                {
                    if (parentId == 0)
                    {
                        TipCategoriesLB.ItemsSource = TipCategories.Where(o => o.ParentCategoryID == null);
                    }
                    else
                    {
                        TipCategoriesLB.ItemsSource = TipCategories.Where(o => o.ParentCategoryID == parentId);
                    }
                });
            
        }

        private void TipCategoriesLB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (TipCategoriesLB.SelectedItem != null)
            {
                string categoryName = ((TipSync.TipCategories)TipCategoriesLB.SelectedItem).CategoryName;
                int id = (int)((TipSync.TipCategories)TipCategoriesLB.SelectedItem).TipCategoryID;
                SelectedCategoryLBL.Text = categoryName;
                SetTipCategoriesItemSource(id);
                TipsLB.ItemsSource = tips.Where(u => u.TipCategoryID == id);
            }
        }

        private int GetTipCategoryParentID(string tipCategory)
        {
            TipSync.TipCategories cat= TipCategories.Where(o => o.CategoryName == tipCategory).SingleOrDefault();
            if (cat != null)
            {
                if (cat.ParentCategoryID != null)
                {
                    return (int)cat.ParentCategoryID;
                }
            }
            return 0;
        }

        private int GetTipCategoryID(string tipCategory)
        {
            TipSync.TipCategories cat = TipCategories.Where(o => o.CategoryName == tipCategory).SingleOrDefault();
            if (cat != null)
            {
                    return (int)cat.TipCategoryID;
            }
            return 0;
        }

        private string GetCategoryName(int id)
        {
            if (id != 0)
            {
                return TipCategories.Where(o => o.TipCategoryID == id).SingleOrDefault().CategoryName;
            }
            return "";
        }

        private void SelectedCategoryLBL_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            int id=GetTipCategoryParentID(SelectedCategoryLBL.Text);
            SetTipCategoriesItemSource(id);
            SelectedCategoryLBL.Text = GetCategoryName(id);
            
        }

        private void Like_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            
        }

        private void Subscribe_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {

        }

        
        
        
        
        
        
        
        
        
        

        


    }
}